﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Util.Classes
{
    /// <summary>
    /// represents a bomb that is drawn on specific map cell
    /// </summary>
    public class BombToDraw
    {
        #region properties
        public string BombID; //name of the bomb
        public List<Classes.BombPosition> AvailablePositions;
        public Vector2 Position;

        public bool IsHidden; //if true, player needs to locate the bomb first ,before defuse can be done
        public bool MustBeVisbleToDefuse; //if true, the bomb needs to be visible to be defused

        public bool Found;
        public bool Defused;
        public bool AvatarNearTheBomb;
        public int Tries;
        public bool Trackable; //if trackable by scanner

        //width and height of the obstacle
        public int[] Dimensions;
        public bool DimensionsSet;
        #endregion


        #region public fields
        #endregion


        #region public methods

        /// <summary>
        /// default constructor
        /// </summary>
        public BombToDraw()
        {
            Tries = 3; //THIS SHOULD BE LOADED FROM STORAGE, IF EXIST IN THERE
            Trackable = false;
        }

        /// <summary>
        /// Choose one of the 
        /// multiple available bomb positions
        /// </summary>
        public void SetPosition()
        {
            try
            {
                if (this.AvailablePositions.Count == 1)
                {
                    //set first and only position
                    this.Position = new Vector2(this.AvailablePositions[0].X, this.AvailablePositions[0].Y);
                }


            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }

        /// <summary>
        /// Get width of the bomb
        /// with the specified ID
        /// </summary>
        /// <returns></returns>
        public double GetBombWidth()
        {
            try
            {
                //loop throug available bombs and get width of the bomb
                //with the same id
                for (int i = 0; i < Levels.LevelManager.CurrentMap.AvailableBombs.Count; i++)
                {
                    if (Levels.LevelManager.CurrentMap.AvailableBombs[i].Name.ToLower() == this.BombID.ToLower())
                    {
                        return Levels.LevelManager.CurrentMap.AvailableBombs[i].Width;
                    }

                }

                return 0;


            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return 0;
            }
        }


        /// <summary>
        /// Get width of the bomb
        /// with the specified ID
        /// </summary>
        /// <returns></returns>
        public double GetBombHeight()
        {
            try
            {
                //loop throug available bombs and get width of the bomb
                //with the same id
                for (int i = 0; i < Levels.LevelManager.CurrentMap.AvailableBombs.Count; i++)
                {
                    if (Levels.LevelManager.CurrentMap.AvailableBombs[i].Name.ToLower() == this.BombID.ToLower())
                    {
                        return Levels.LevelManager.CurrentMap.AvailableBombs[i].Height;
                    }

                }

                return 0;


            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return 0;
            }
        }

        /// <summary>
        /// is avatar near the bomb?
        /// </summary>
        /// <returns></returns>
        public bool IsAvatarNearTheBomb()
        {
            int[] rectangleValues;

            try
            {
                rectangleValues = new int[] { (int)Position.X - World.WorldManager.WorldConfig.NodeWidth, (int)Position.Y - World.WorldManager.WorldConfig.NodeHeight, World.WorldManager.WorldConfig.NodeWidth * 3, World.WorldManager.WorldConfig.NodeHeight * 3 };

                //is avatar intersecting with the bomb check rectangle?
                if (new Rectangle((int)Util.Cache.Avatar.CurrentPosition.X, (int)Util.Cache.Avatar.CurrentPosition.Y, Util.Cache.Avatar.Width, Util.Cache.Avatar.Height).Intersects(new Rectangle(rectangleValues[0], rectangleValues[1], rectangleValues[2], rectangleValues[3])))
                {
                    rectangleValues = null;
                    return true;
                }

                rectangleValues = null;
                return false;
            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                rectangleValues = null;
                return false;
            }
        }


        #region DRAW

        /// <summary>
        /// draw the bomb
        /// </summary>
        public void Draw()
        {
            try
            {

                if (!IsHidden && !Defused)
                {
                    //draw the bomb not defused yet
                    if (IsAvatarNearTheBomb())
                    {
                        //draw the bomb active
                        Util.Cache.Draw(Util.Cache.GetTexture(this.BombID).Text2D, new int[] { (int)this.Position.X, (int)this.Position.Y, Dimensions[0], Dimensions[1]}, Color.White);
                        AvatarNearTheBomb = true;
                    }
                    else
                    {
                        //draw the bomb not active
                        Util.Cache.Draw(Util.Cache.GetTexture(this.BombID + "Inactive").Text2D, new int[] { (int)this.Position.X, (int)this.Position.Y, Dimensions[0], Dimensions[1] }, Color.White);
                        AvatarNearTheBomb = false;
                    }

                    //draw locked or unlocked icon next to bomb
                    if (Levels.LevelManager.CurrentMap.ChosenSecretWord.Solved)
                    {
                        //unlocked
                        Util.Cache.Draw(Util.Cache.GetTexture("BombUnlocked").Text2D, new int[] { (int)this.Position.X + (int)GetBombWidth(), (int)this.Position.Y, Dimensions[0] / 2, Dimensions[1] / 2 }, Color.White);
                    }
                    else
                    {
                        //locked
                        Util.Cache.Draw(Util.Cache.GetTexture("BombLocked").Text2D, new int[] { (int)this.Position.X + (int)GetBombWidth(), (int)this.Position.Y, Dimensions[0] / 2, Dimensions[1] / 2 }, Color.White);
                    }

                }
                //else if (!IsHidden && Defused)
                //{
                    //IF THE BOMB IS DEFUSED, THEN DO NOT DRAW
                //}

            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }


        #endregion


        #endregion
    }
}
